﻿/*******************************************************************************
 * AAEngine
 * Copyright (c) 2010 Mike Jarosch
 *
 * Original source PushButton Engine:
 * Copyright (C) 2009 PushButton Labs, LLC
 * For more information see http://www.pushbuttonengine.com
 *
 * This file is licensed under the terms of the MIT license, which is included
 * in the "MIT License.txt" file at the root directory of this SDK.
 ******************************************************************************/

using Microsoft.Xna.Framework;
using Box2D.XNA;

namespace AAEngine.Box2D
{
    public class PolygonCollisionShape : CollisionShape
    {
        Vector2[] _vertices;

        public Vector2[] Vertices
        {
            get { return _vertices; }
            set
            {
                _vertices = value;

                if (_parent != null)
                {
                    _parent.BuildCollisionShapes();
                }
            }
        }

        protected override FixtureDef DoCreateFixture()
        {
            Vector2 halfSize = _parent.Size * 0.5f;
            float scale = _parent.SpatialManager.InverseScale;

            var fixture = base.DoCreateFixture();

            PolygonShape shape = new PolygonShape();

            int i = 0;
            foreach(Vector2 vertex in _vertices)
            {
                shape._vertices[i] = vertex * halfSize * scale;
                i++;
            }
            shape._vertexCount = i;

            fixture.shape = shape;

            return fixture;
        }
    }
}
